function validateEmail(email) { 
  // http://stackoverflow.com/a/46181/11236
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
function IsNumeric(input)
{
    return (input - 0) == input && (input+'').replace(/^\s+|\s+$/g, "").length > 0;
}

/* loaded in tpl1-condition-elements.phtml*/
_conditionMap = {
}

_CAR = {
  validateStep8: function ()
  {
    var errors = "";

	if (!$("#appointment_date").val())
	  errors += "Please select your appointment date\n";
	
	$("#ap_date").val($("#appointment_date").val());

	if (errors)
	{
      alert(errors);
      return false;
	}

	return true;
  },
  validateStep7: function ()
  {
    var errors = "";

	if (!$("#name").val())
	  errors += "Please enter your name\n";

	if (!$("#email").val())
	  errors += "Please enter your email\n";
	else if (!validateEmail($("#email").val()))
	  errors += "Please enter your valid email\n";
	
	if (!$("#postcode").val())
	  errors += "Please enter your postcode\n";

	if (errors)
	{
      alert(errors);
      return false;
	}

	return true;
  },
  validateStep6: function ()
  {
    var errors = "";

	if (!$("#happy_to_receive_this_information:checked").val())
	  errors += "Please accept the terms and conditions\n";

	if (errors)
	{
      alert(errors);
      return false;
	}

	return true;
  },
  validateStep5: function ()
  {
    var errors = "";

	if (!$("input[name='mechanical_condition']:checked").val())
	  errors += "Please select mechanical condition\n";

	if (!$("input[name='interior_condition']:checked").val())
	  errors += "Please select Interior condition\n";

	if (!errors && _CAR.conditionSummaryList.length == 0)
	{
	   return confirm("You didn't select any damages to the body. You can add damages by clicking on the image and selecting the damage type. Do you confirm that the Body is in perfect condition?");
	}

	if (errors)
	{
      alert(errors);
      return false;
	}

	return true;
  },
  validateStep4: function ()
  {
    var errors = "";
    if ($("#major_selling_points").val() == "")
	  errors += "Please fill major selling points\n";

	if (errors)
	{
      alert(errors);
      return false;
	}

	return true;
  
  },
  validateStep3: function ()
  {
    var errors = "";
    if ($("#mileage").val() == "")
	  errors += "Please enter mileage\n";
    else if (!IsNumeric($("#mileage").val()))
	  errors += "Please enter valid mileage\n";

	if (!$("input[name='prev_owners']:checked").val())
	  errors += "Please select number of previous owners\n";
	
	if (!$("input[name='service_history']:checked").val())
	  errors += "Please select service history\n";
	
	if (!$("input[name='is_scrap']:checked").val() || !$("input[name='left_hand_drive']:checked").val())
	  errors += "Please answer all questions\n";

	if (errors)
	{
      alert(errors);
      return false;
	}

	return true;
  },
  conditionSelectedTable : 12,
  conditionShowTable : function (tID)
  {
    for (i = 0; i <= 27; i++)
      $("#t_"+i).hide();

	_CAR.conditionSelectedTable = tID;

    $("#t_"+tID).show();
  },
  /*
    list of car losses
  */
  conditionSummaryList : [],
  conditionAdd : function (id, text)
  {
    _CAR.conditionSummaryList[ id ] = { 'text' : text, 'table' : _CAR.conditionSelectedTable, 'id' : id };
    _CAR.conditionPrint();
  },
  conditionRemove : function (id)
  {
     delete _CAR.conditionSummaryList[ id ];
    _CAR.conditionPrint();
  },
  conditionPrint : function ()
  {
    var outHtml = "";
    for (id in _CAR.conditionSummaryList)
	{
	  elem = _CAR.conditionSummaryList[id];

      outHtml += "<a href='javascript:_CAR.conditionRemove("+elem.id+")' class='condElem'>"+_conditionMap[elem.table]+ ", " +elem.text+"</div>";
	}
    $("#summary-list").html(outHtml);
  }
}

var conditionSummary = new Array();
function show_hid(id, text)
{
  _CAR.conditionAdd(id, text)
}

$( function () {
 $("#mileage").keydown(function () {
      $(this).val(
	    $(this).val().substring(0, 6)
	  ) 
 });
});

